import { ExceptionFilter, Catch, ArgumentsHost } from '@nestjs/common'
import { CoreException } from '@/modules'
import moment from 'moment'
import { CustomLoggerService } from '../logger'

@Catch(CoreException)
export class CoreExceptionFilter implements ExceptionFilter {
    constructor(private readonly logger: CustomLoggerService) {}
    public catch(exception: CoreException, host: ArgumentsHost) {
        const ctx = host.switchToHttp()
        const response = ctx.getResponse()
        const request = ctx.getRequest()
        const status = exception.getStatus()
        this.logger.filterLog(host, null, exception.message)
        response.status(200).json({
            flag: status,
            timestamp: moment().format('YYYY-MM-DD HH:mm:ss'),
            path: request.url,
            message: exception.message,
        } as any)
    }
}
